import 'package:get/get.dart';
import 'package:flutter_base/http/http_connection_check.dart';
import 'package:flutter_base/utils/log/utils_log.dart';

enum ViewStatus {
  unknown, //未知
  loading, //加载中
  finished, //完成加载
  error, //接口错误
  empty, //无数据
  noNetwork, //无网络
}

class BaseController extends FullLifeCycleController {
  ViewStatus _viewStatus = ViewStatus.unknown;

  ViewStatus get viewStatus => _viewStatus;

  set viewStatus(ViewStatus value) {
    _viewStatus = value;
    update();
  }

  @override
  void onInit() {
    clog("===> ${runtimeType.toString()} init");
    super.onInit();
    if (isNoNetWork) {
      doNoNetwork();
    }
  }

  @override
  void onReady() {
    clog("===> ${runtimeType.toString()} onReady");
    super.onReady();
  }

  @override
  void onClose() {
    clog("===> ${runtimeType.toString()} close");
    super.onClose();
  }

  void executeInitMethod(Future Function() initMethod) {
    doLoading();
    initMethod.call().then((value) {
      doFinished();
    }, onError: (error) {
      if (isNoNetWork) {
        doNoNetwork();
      } else {
        doError(error);
      }
    });
  }

  void doLoading() {
    viewStatus = ViewStatus.loading;
  }

  void doFinished() {
    viewStatus = ViewStatus.finished;
  }

  void doUpdate() {
    viewStatus = ViewStatus.finished;
  }

  void doError(dynamic error) {
    viewStatus = ViewStatus.error;
  }

  void doEmpty() {
    viewStatus = ViewStatus.empty;
  }

  void doNoNetwork() {
    viewStatus = ViewStatus.noNetwork;
  }

  bool get isLoading => viewStatus == ViewStatus.loading;
}
